package com.sduept.nwld.dataserver.sourceinfo.transform.impl;

import com.sduept.nwld.dataserver.sourceinfo.transform.TransformFunction;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class Replace implements TransformFunction<String>{
	private Map<String,Object> params;
	public Replace(){
		if(params==null)
			params=new HashMap<String,Object>();	
	}

	@Override
	public Map<String,Object> getParams() {				
		return params;
	}	

	@Override
	public String apply(Map<String, Object> params) {		
		String sourceInfo="";		
		int replaceType=0;
		Object oldInfo=null; 
		String regEx=null;
		Object newInfo=null;
		String targetInfo=null;
		if(params!=null){
			if(params.size()==4){				
				for(String paramName:params.keySet()){					
					if("sourceInfo".equals(paramName)&& params.get(paramName) instanceof String){ 						
						sourceInfo=params.get(paramName).toString();
					}else if("replaceType".equals(paramName)&& params.get(paramName) instanceof Integer){
						replaceType=(int)params.get(paramName);
					}else if("oldInfo".equals(paramName)&& params.get(paramName) instanceof Character){
						oldInfo=(char)params.get(paramName);
					}else if("oldInfo".equals(paramName)&& params.get(paramName) instanceof String){
						oldInfo=params.get(paramName).toString();
					}else if("newInfo".equals(paramName)&& params.get(paramName) instanceof Character){
						newInfo=(char)params.get(paramName);						
					}else if("newInfo".equals(paramName)&& params.get(paramName) instanceof String){
						newInfo=params.get(paramName).toString();
					}					
				}				
				if(oldInfo instanceof Character){
					if(sourceInfo.indexOf((char)oldInfo)!=-1){
						if(replaceType==1){
							if(newInfo instanceof Character)
								targetInfo=sourceInfo.replaceFirst(String.valueOf(oldInfo), String.valueOf(newInfo));	
							else if(newInfo instanceof String)
								targetInfo=sourceInfo.replaceFirst(String.valueOf(oldInfo), newInfo.toString());	
						}else if(replaceType==2){
							if(newInfo instanceof Character)
								targetInfo=sourceInfo.replace((char)oldInfo, (char)newInfo);
							else if(newInfo instanceof String)
								targetInfo=sourceInfo.replaceAll(String.valueOf(oldInfo), newInfo.toString());	
						}
					}
				}else if(oldInfo instanceof String){
					if(sourceInfo.indexOf(oldInfo.toString())!=-1){
						if(replaceType==1){					
							targetInfo=sourceInfo.replaceFirst(String.valueOf(oldInfo), String.valueOf(newInfo));				
						}else if(replaceType==2){
							if(newInfo instanceof Character)
								targetInfo=sourceInfo.replace(oldInfo.toString(), String.valueOf(newInfo));
							else if (newInfo instanceof String)
								targetInfo=sourceInfo.replace(oldInfo.toString(),newInfo.toString());
							
						}
					}
				}		
			}else if(params.size()==3){
				for(String paramName:params.keySet()){
					if("sourceInfo".equals(paramName)&& params.get(paramName) instanceof String){ 						
						sourceInfo=params.get(paramName).toString();
					}else if("regEx".equals(paramName)&& params.get(paramName) instanceof String){
						regEx=params.get(paramName).toString();
					}else if("newInfo".equals(paramName)&& params.get(paramName) instanceof Character){
						newInfo=(char)params.get(paramName);						
					}else if("newInfo".equals(paramName)&& params.get(paramName) instanceof String){
						newInfo=params.get(paramName).toString();
					}		
				}
				if(newInfo instanceof Character){			
					targetInfo=sourceInfo.replaceAll(regEx, String.valueOf(newInfo));			
				}else if(newInfo instanceof String){
					targetInfo=sourceInfo.replaceAll(regEx, newInfo.toString());	
				}
			}			
				
		}		
		return targetInfo;
	}
	
	
	public static void main(String[] args){
		Replace replace=new Replace();		
		String sourceInfo="abcde5fe3sec";		
		int replaceType=2;
		Object oldInfo='e'; 
		String regEx="\\d";
		Object newInfo="****";
		String targetInfo=null;
		replace.params.put("sourceInfo", sourceInfo);
		/*replace.params.put("replaceType",replaceType);
		replace.params.put("oldInfo",oldInfo);*/
		replace.params.put("regEx",regEx);
		replace.params.put("newInfo",newInfo);
		targetInfo=replace.apply(replace.params);
	    log.info(targetInfo);
	
	}


}
